package singleton;

/**
 * 懒汉式单例
 * 线程安全
 */
public class LazySingleton {
    private static volatile LazySingleton instance;
    
    private LazySingleton() {
        // 防止反射攻击
        if (instance != null) {
            throw new RuntimeException("单例对象不能重复创建");
        }
    }
    
    public static LazySingleton getInstance() {
        if (instance == null) {
            synchronized (LazySingleton.class) {
                if (instance == null) {
                    instance = new LazySingleton();
                }
            }
        }
        return instance;
    }
}